<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>authenticate (HomeController)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File app/controllers/home_controller.rb, line 27</span>
<span class="ruby-keyword kw">def</span> <span class="ruby-identifier">authenticate</span> 
        <span class="ruby-comment cmt">#User.new(params[:userform]) will create a new object of User, retrieve values from the form and store it variable @user. </span>
        <span class="ruby-ivar">@user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">params</span>[<span class="ruby-identifier">:userform</span>]) 
        <span class="ruby-comment cmt">#find records with username,password </span>
        <span class="ruby-identifier">valid_user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">:first</span>,<span class="ruby-identifier">:conditions</span> =<span class="ruby-operator">&gt;</span> [<span class="ruby-value str">&quot;username = ? and password = ?&quot;</span>,<span class="ruby-ivar">@user</span>.<span class="ruby-identifier">username</span>, <span class="ruby-ivar">@user</span>.<span class="ruby-identifier">password</span>]) 
 
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">valid_user</span>
          <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">valid_user</span>.<span class="ruby-identifier">username</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;admin&quot;</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">valid_user</span>.<span class="ruby-identifier">password</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;admin&quot;</span>
          <span class="ruby-identifier">flash</span>[<span class="ruby-identifier">:notice</span>] = <span class="ruby-value str">&quot;welcome admin&quot;</span>
          <span class="ruby-identifier">session</span>[<span class="ruby-identifier">:user_id</span>]=<span class="ruby-identifier">valid_user</span>.<span class="ruby-identifier">id</span>
          <span class="ruby-identifier">redirect_to</span> <span class="ruby-identifier">:action</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'admin'</span> <span class="ruby-comment cmt">###and return</span>
          <span class="ruby-keyword kw">else</span>
            <span class="ruby-identifier">flash</span>[<span class="ruby-identifier">:notice</span>] = <span class="ruby-value str">&quot;welcome registered user&quot;</span>
            <span class="ruby-identifier">session</span>[<span class="ruby-identifier">:user_id</span>]=<span class="ruby-identifier">valid_user</span>.<span class="ruby-identifier">id</span>   <span class="ruby-comment cmt">#####</span>
            <span class="ruby-identifier">redirect_to</span> <span class="ruby-identifier">:action</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'private'</span> 
          <span class="ruby-keyword kw">end</span>   
          
        <span class="ruby-keyword kw">else</span> 
            <span class="ruby-identifier">flash</span>[<span class="ruby-identifier">:notice</span>] = <span class="ruby-value str">&quot;Invalid User/Password combination&quot;</span> 
            <span class="ruby-identifier">redirect_to</span> <span class="ruby-identifier">:action=</span><span class="ruby-operator">&gt;</span> <span class="ruby-value str">'index'</span> <span class="ruby-comment cmt">###and return           </span>
        <span class="ruby-keyword kw">end</span>
          
                  
        
  <span class="ruby-keyword kw">end</span></pre>
</body>
</html>